package com.guinea.account.web;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;

import com.guinea.account.entity.Dictionary;
import com.guinea.account.form.DictionaryForm;
import com.guinea.account.service.DictionaryManager;
import com.guinea.core.service.CRUDManager;
import com.guinea.core.web.StrutsEntityAction;

public class DictionaryAction extends StrutsEntityAction {

	private DictionaryManager dictionaryManager;

	public CRUDManager getEntityManager() {
		return (CRUDManager) getDictionaryManager();
	}

	public DictionaryManager getDictionaryManager() {
		return dictionaryManager;
	}

	public void setDictionaryManager(DictionaryManager dictionaryManager) {
		this.dictionaryManager = dictionaryManager;
	}

	/**
	 * 保存Form表单时的回调函数.为业务对象添加更多属性，在子类重载.
	 */
	public void onInitEntity(ActionForm form, HttpServletRequest request,
			Object object) {
		String s = request.getParameter("code");
		Dictionary d = (Dictionary)object;
		//fix ActionForm bug
		if(s != null)
			d.setCode(s);
		//设置parentId默认值
		s = request.getParameter("parentId");
		if(s == null)
			d.setParentId(new Long(0L));
			
	}

	/**
	 * 显示Form表单时的回调函数.为Form对象添加更多属性,在子类重载.
	 */
	public void onInitForm(ActionForm form, HttpServletRequest request,
			Object object) {
		if (id != null) {
			DictionaryForm dictionaryForm = (DictionaryForm) form;
			DictionaryManager menuManager = getDictionaryManager();
			dictionaryForm.setChildrenIds(menuManager.getChildrenIds(id));
		}
	}
	/**
	 * 保存业务对象的函数.
	 */
	public void doSaveEntity(HttpServletRequest request,
			Object object) {
		getDictionaryManager().save(object);
		reset(request);
	}

	/**
	 * 标记删除业务对象的函数.
	 */
	public void doEraseEntity(HttpServletRequest request,
			Object object) {
		getDictionaryManager().save(object);
		reset(request);
	}

	/**
	 * 删除业务对象的函数.
	 */
	public void doDeleteEntity(HttpServletRequest request,
			Object object) {
		getDictionaryManager().delete(object);
		reset(request);
	}
	private void reset(HttpServletRequest request){
		request.getSession().getServletContext().setAttribute("rootDictionary", getDictionaryManager().getRootNodes());
		request.getSession().getServletContext().setAttribute("leafDictionary", getDictionaryManager().getLeafNodes());
	}
}
